package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.log.Logger;
import ru.compscicenter.uno.model.Rules;
import ru.compscicenter.uno.util.ImagePool;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * User: SLeeP
 * Date: 07.03.12
 * Time: 4:02
 */
public class Panel3ClientRules extends JPanel implements MouseListener {
    private final static int TOTAL_RULES = Panel2ServerRules.TOTAL_RULES;
    public static boolean start = false;

    private JLabel blankSpace;
    private JLabel blankSpace2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel networkInterfaceLabel;
    private static JCheckBox[] rule;
    private static JTextField pointsToWinText;

    private JLabel welcomeTitle;
    private JPanel contentPanel;
    private JTextArea rulesHelp;

    private JLabel iconLabel;
    private ImageIcon icon;


    public Panel3ClientRules() {

        iconLabel = new JLabel();
        contentPanel = getContentPanel();
        contentPanel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

        icon = new ImageIcon(ImagePool.getImage(Wizard.unoIconDIR, 250, 383));

        setLayout(new java.awt.BorderLayout());

        if (icon != null)
            iconLabel.setIcon(icon);

        iconLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED));

        add(iconLabel, BorderLayout.WEST);

        JPanel secondaryPanel = new JPanel();
        secondaryPanel.add(contentPanel, BorderLayout.NORTH);
        add(secondaryPanel, BorderLayout.CENTER);
    }

    private JPanel getContentPanel() {

        JPanel contentPanel1 = new JPanel();
        JPanel jPanel1 = new JPanel();

        welcomeTitle = new JLabel();
        blankSpace = new JLabel();
        blankSpace2 = new JLabel();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        rulesHelp = new JTextArea(5, 10);
        rule = new JCheckBox[TOTAL_RULES];
        for (int i = 0; i != TOTAL_RULES; i++) {
            rule[i] = new JCheckBox();
        }
        pointsToWinText = new JTextField();
        Panel2ServerRules.setRuleNames(rule);
        setInactive();

        contentPanel1.setLayout(new java.awt.BorderLayout());

        welcomeTitle.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 14));
        welcomeTitle.setText("Rules on this server:");
        contentPanel1.add(welcomeTitle, java.awt.BorderLayout.NORTH);

        jPanel1.setLayout(new java.awt.GridLayout(0, 2));
        jPanel1.add(blankSpace);
        jPanel1.add(blankSpace2);
        jPanel1.add(new JLabel("Points to win: "));
        jPanel1.add(pointsToWinText);

        for (int i = 0; i != TOTAL_RULES; i++) {
            jPanel1.add(rule[i]);
        }

        jPanel1.add(new JLabel());
        rulesHelp.setEditable(false);
        contentPanel1.add(jPanel1, java.awt.BorderLayout.CENTER);
        contentPanel1.add(rulesHelp, BorderLayout.SOUTH);

        return contentPanel1;

    }

    public void setInactive() {
        pointsToWinText.setEnabled(false);
        for (int i = 0; i != TOTAL_RULES; i++) {
            rule[i].setEnabled(false);
            rule[i].addMouseListener(this);
        }
    }

    public static void setRules(Rules rules) {
        pointsToWinText.setText(rules.getPointsToWin() + "");
        rule[0].setSelected(rules.isActivated(Rules.Rule.THROW));
        rule[1].setSelected(rules.isActivated(Rules.Rule.THROW_ONLY_NUMERIC));
        rule[2].setSelected(rules.isActivated(Rules.Rule.SEVEN_ZERO_EXCHANGE));
        rule[3].setSelected(rules.isActivated(Rules.Rule.TRANSFER));
        rule[4].setSelected(rules.isActivated(Rules.Rule.FIVE_EXCHANGE));
        rule[5].setSelected(rules.isActivated(Rules.Rule.LAZY_SCORING));
        rule[6].setSelected(rules.isActivated(Rules.Rule.UNO_IN_CENTER));
        rule[7].setSelected(rules.isActivated(Rules.Rule.COLOR_BY_CIRCLE));
        rule[8].setSelected(rules.isActivated(Rules.Rule.REVERSE));
        rule[9].setSelected(rules.isActivated(Rules.Rule.LAST_HERO));
        rule[10].setSelected(rules.isActivated(Rules.Rule.SEVEN_ROUNDS));
        rule[11].setSelected(rules.isActivated(Rules.Rule.DROP_ZERO));
        rule[12].setSelected(rules.isActivated(Rules.Rule.EIGTH_EXTRA));
        rule[13].setSelected(rules.isActivated(Rules.Rule.TWO_IN_CENTER));
        rule[14].setSelected(rules.isActivated(Rules.Rule.RED_ZERO));
        rule[15].setSelected(rules.isActivated(Rules.Rule.FIRST_LOOSER));
    }

    private void setRuleNames() {
        rule[0].setText("Interference");
        rule[1].setText("Put 2 equal cards");
        rule[2].setText("Uno 7-0");
        rule[3].setText("With the accumulation");
        rule[4].setText("Card exchange");
        rule[5].setText("Lazy scoring");
        rule[6].setText("To the center!");
        rule[7].setText("Colors by circle");
        rule[8].setText("Reverse");
        rule[9].setText("Till the last");
        rule[10].setText("In 7 rounds");
        rule[11].setText("Drop to 0");
        rule[12].setText("8 = extra move");
        rule[13].setText("2 = to the center");
        rule[14].setText("Red zero");
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getComponent().equals(rule[0])) {
            rulesHelp.setText("info about \"Interference\" rule here");
        } else if (e.getComponent().equals(rule[1])) {
            rulesHelp.setText("info about \"Put 2 equal cards\" rule here");
        } else if (e.getComponent().equals(rule[2])) {
            rulesHelp.setText("info about \"Uno 7-0\" rule here");
        } else if (e.getComponent().equals(rule[3])) {
            rulesHelp.setText("info about \"With the accumulation\" rule here");
        } else if (e.getComponent().equals(rule[4])) {
            rulesHelp.setText("info about \"Card exchange\" rule here");
        } else if (e.getComponent().equals(rule[5])) {
            rulesHelp.setText("info about \"Lazy scoring\" rule here");
        } else if (e.getComponent().equals(rule[6])) {
            rulesHelp.setText("info about \"To the center!\" rule here");
        } else if (e.getComponent().equals(rule[7])) {
            rulesHelp.setText("info about \"Colors by circle\" rule here");
        } else if (e.getComponent().equals(rule[8])) {
            rulesHelp.setText("info about \"Reverse\" rule here");
        } else if (e.getComponent().equals(rule[9])) {
            rulesHelp.setText("info about \"Till the last\" rule here");
        } else if (e.getComponent().equals(rule[10])) {
            rulesHelp.setText("info about \"In 7 rounds\" rule here");
        } else if (e.getComponent().equals(rule[11])) {
            rulesHelp.setText("info about \"Drop to 0\" rule here");
        } else if (e.getComponent().equals(rule[12])) {
            rulesHelp.setText("info about \"8 = extra move\" rule here");
        } else if (e.getComponent().equals(rule[13])) {
            rulesHelp.setText("info about \"2 = to the center\" rule here");
        } else if (e.getComponent().equals(rule[14])) {
            rulesHelp.setText("info about \"Red zero\" rule here");
        } else if (e.getComponent().equals(rule[15])) {
            rulesHelp.setText("info about \"Alternative scoring\" rule here");
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        rulesHelp.setText("");
    }

}
